package My

import (
	"fmt"
	"db2s/ETL"
	"strings"
)

// slowLowSwitch 检查MySQL慢日志是否开启
func (sl *CollectSlowLog) slowLogSwitch() {
	var (
		strSql      string
		slowLogOpen = false
		err         error
	)
	sl.Event = "mysql_slow_log_switch"
	strSql = fmt.Sprintf("show global variables like \"slow_query_log\"")
	active := ETL.InstanceActiveS{DBType: DBType, DB: sl.Db["single"], StopTime: sl.SqlExecStopTime}
	var f1 any
	if _, f1, err = active.SqlQuery(strSql, ETL.Map); err != nil {
		WLog.Error(fmt.Sprintf("(%d) %v get slow log parameter fail. error info is {%v}", sl.LogSeq, sl.Event, err))
		return
	}
	if v, ok := f1.(map[string]any)["slow_query_log"]; ok {
		switch strings.ToLower(v.(string)) {
		case "off":
			slowLogOpen = false
		case "on":
			slowLogOpen = true
		}
	}
	if !slowLogOpen {
		return
	}
	strSql = fmt.Sprintf("show global variables like \"slow_query_log_file\"")
	if _, f1, err = active.SqlQuery(strSql, "map"); err != nil {
		WLog.Error(fmt.Sprintf("(%d) %v get slow log parameter fail. error info is {%v}", sl.LogSeq, sl.Event, err))
		return
	}
	sl.SlowLogPath = f1.(map[string]any)["slow_query_log_file"].(string)
}
